package com.qyhl.module_home.city.bestone;


import com.qyhl.module_home.app.HomeApplication;
import com.qyhl.module_home.common.HomeUrl;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.home.BestoneItemTypeBean;
import com.qyhl.webtv.commonlib.entity.home.BestoneTypeBean;

import java.util.List;

public class BestoneModel implements BestoneContract.BestoneModel {
    private BestoneContract.BestonePresenter presenter;

    public BestoneModel(BestoneContract.BestonePresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void getData() {
        if (NetUtil.isNetworkAvalible(HomeApplication.getApp())) {

            EasyHttp.get(HomeUrl.GET_BESTONE_CALSSTYPE)
                    .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                    .execute(new SimpleCallBack<List<BestoneTypeBean>>() {
                        @Override
                        public void onError(ApiException e) {
                            presenter.getDataError("网络繁忙，请稍后再试");
                        }

                        @Override
                        public void onSuccess(List<BestoneTypeBean> bestoneTypeBeans) {
                            presenter.getDataSuccess(bestoneTypeBeans);
                        }
                    });

        } else {
            presenter.getDataError("无网络连接，请检查您的网络...");
        }
    }

    @Override
    public void search(String text) {
        if (NetUtil.isNetworkAvalible(HomeApplication.getApp())) {

            EasyHttp.get(HomeUrl.GET_BESTONE_SEARCH)
                    .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                    .params("content", text)
                    .execute(new SimpleCallBack<List<BestoneItemTypeBean>>() {
                        @Override
                        public void onError(ApiException e) {
                            presenter.searchError("网络繁忙，请稍后再试");
                        }

                        @Override
                        public void onSuccess(List<BestoneItemTypeBean> bestoneItemTypeBeans) {
                            presenter.searchSuccess(bestoneItemTypeBeans);
                        }
                    });

        } else {
            presenter.searchError("无网络连接，请检查您的网络...");
        }
    }
}
